<?php
/**
 * FastApp.
 * 11/18/24
 * @Author 深色的海 (wx:deepsea_0024)
 * @Contact telegram:deepsea159
 */

namespace App\Marketing\Api\Service;

use App\Fastapp\Api\Service\Account\WalletService;
use App\Fastapp\Exception\TipException;
use App\Fastapp\Model\AccountWalletLog;
use App\Marketing\Model\MarketingCashPrize;
use App\Marketing\Model\MarketingCashPrizeLog;
use Carbon\Carbon;

class CashprizeService
{
    public function detail(?string $code): array
    {
        $map['status'] = 0;
        $map['account_type'] = fastapp_header('account-type', 1);
        if ($code) $map['code'] = $code;
        $md = MarketingCashPrize::query()
            ->select(['id', 'symbol', 'no', 'num', 'status', 'start_at', 'end_at', 'min', 'max'])
            ->where($map)
            ->first();
        if (!$md) return [];
        return $md->toArray();
    }

    public function getRefunded(?string $no): array
    {
        $uid = get_uid();
        $map['no'] = $no;
        $map['account_type'] = fastapp_header('account-type', 1);
        $prize = MarketingCashPrize::query()->where($map)->firstOrFail();
        if ($prize->start_at && $prize->end_at) {
            $t = Carbon::now();
            if ($t->lt($prize->start_at) || $t->gt($prize->end_at)) throw new TipException(fastapp_t('marketing.activity_is_over'));
        }
        if (MarketingCashPrizeLog::query()->where(['user_id' => $uid, 'prize_id' => $prize->id])->exists()) throw new TipException(fastapp_t('marketing.received'));
        if ($prize->status) {
            redis()->del('cap:' . $prize->id);
            throw new TipException(fastapp_t('fastapp.invalid_operation'));
        }
        $k = 'cashprize:' . $uid;
        if (redis()->setnx($k, 1)) {
            redis()->expire($k, 3);
            $n = redis()->incr('cap:' . $prize->id);
            if ($n > $prize->num) {
                MarketingCashPrize::query()->where(['id' => $prize->id])->update(['status' => 1]);
                redis()->del('cap:' . $prize->id);
                throw new TipException(fastapp_t('marketing.activity_is_over'));
            }

            if ($prize->config) {
                $config = [];
                foreach ($prize->config as $item) {
                    $config[] = [$item['start'], $item['end'], $item['rate']];
                }
                $amount = randomByProbability($config);
            } else {
                $amount = mt_rand((int)($prize->min * 100), (int)($prize->max * 100)) / 100;
            }
            $md = MarketingCashPrizeLog::query()->create([
                'user_id' => $uid,
                'prize_id' => $prize->id,
                'status' => 1,
                'amount' => $amount,
            ]);
            WalletService::inc([
                'user_id' => $uid,
                'amount' => $amount,
                'symbol' => $prize->symbol,
                'type' => AccountWalletLog::RECHARGE,
                'object_type' => AccountWalletLog::OBJ_TYPE_CASH_PRIZE_LOG,
                'object_id' => $prize->id
            ]);
            return $md->toArray();
        }
        redis()->expire($k, 3);
        throw new TipException(fastapp_t('fastapp.operate_frequently'));
    }
}